home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Graphics / include / utility.h < prev    next >
Encoding:
Text File  |  1995-03-13  |  3.2 KB  |  61 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    utility.h
  3. //    Date:                    2/25/95
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains common utility functions
  7. //
  8. //------------------------------------------------------------------------------
  9.  
  10. #ifndef    UTILITY
  11. #define    UTILITY
  12.  
  13. //------------------------------------------------------------------------------
  14. //    inline utilitiy functions
  15. //------------------------------------------------------------------------------
  16. inline    real    DegreesToRadians (real angle)                                                                            //    convert degrees to radians
  17. {                                                                                                                                                                //    begin
  18.     return angle * (PI / R(180.0));                                                                                                //    multiply the angle by the conversion factor
  19. }                                                                                                                                                                //    end
  20.  
  21. //------------------------------------------------------------------------------
  22. inline    real    RadiansToDegrees (real angle)                                                                            //    convert radians to degrees
  23. {                                                                                                                                                                //    begin
  24.     return angle * (R(180.0) / PI);                                                                                                //    multiply the angle by the conversion factor
  25. }                                                                                                                                                                //    end
  26.  
  27. //------------------------------------------------------------------------------
  28. inline    real    abs (real v)                                                                                                            //    return the absolute value
  29. {                                                                                                                                                                //    begin
  30.     return (v >= R(0.0)) ? v : -v;                                                                                                //    negate v if it is less than 0
  31. }                                                                                                                                                                //    end
  32.  
  33. //------------------------------------------------------------------------------
  34. inline    real    sgn (real v)                                                                                                            //    return the sign of the value
  35. {                                                                                                                                                                //    begin
  36.     return (v > R(0.0)) ? R(1.0) : (v == R(0.0)) ? R(0.0) : R(-1.0);                            //    return 1, 0, or -1 based on v
  37. }                                                                                                                                                                //    end
  38.  
  39. //------------------------------------------------------------------------------
  40. inline    bool    odd (int v)                                                                                                                //    return whether the number is odd
  41. {                                                                                                                                                                //    begin
  42.     return bool (v & 1);                                                                                                                    //    if the one bit is set, the number is odd
  43. }                                                                                                                                                                //    end
  44.  
  45. //------------------------------------------------------------------------------
  46. inline    real    fract (real v)                                                                                                        //    return the fractional part of v
  47. {                                                                                                                                                                //    begin
  48.     v = v - floor (v);                                                                                                                        //    get the fractional part
  49.     if (v < R(0.0)) v += R(1.0);                                                                                                    //    if it is a negative value, add one
  50.     return v;                                                                                                                                            //    return the result
  51. }                                                                                                                                                                //    end
  52.  
  53. //------------------------------------------------------------------------------
  54. inline    real    UnitRandom (void)                                                                                                    //    return a random number in the range 0 - 1
  55. {                                                                                                                                                                //    begin
  56.     return abs (Random () / real (SHRT_MAX));                                                                            //    return the absolute value of the divided Random number
  57. }                                                                                                                                                                //    end
  58.  
  59. //------------------------------------------------------------------------------
  60.  
  61. #endif    //UTILITY